package com.nerve.android.cloudoffice.app.activity;

import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;
import org.nerve.android.ui.dialog.ConfirmDialog;
import org.nerve.android.ui.dialog.DialogHelper.ConfirmListener;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.nerve.android.cloudoffice.OfficeApplication;
import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.activity.assist.UserSettingActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.common.Resource;
import com.nerve.android.cloudoffice.domain.User;
import com.nerve.android.cloudoffice.service.data.DepartmentData;

/**
 * 
 * @project Cloudoffice SettingActivity.java
 * @datetime Aug 12, 2013 12:50:37 PM
 * @description 
 *    
 * @author 集成显卡 IBM 
 *			zxingming@qq.com
 *			zengxm@cn.ibm.com
 *
 * @change log:
 *
 */
@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_setting)
public class SettingActivity extends NavigationActivity implements OnClickListener, ConfirmListener{

	@ViewOnId(id=R.id.name)
	protected EditText nameET;
	@ViewOnId(id=R.id.password)
	protected EditText passwordET;
	
	@ViewOnId(id=R.id.okBtn, clickListener="this")
	protected Button loginBtn;
	
	@ViewOnId(id=R.id.logout, clickListener="this")
	protected Button logoutBtn;
	
	@ViewOnId(id=R.id.userLayout, clickListener="this")
	protected View userView;
	
	protected SharedPreferences shareP;
	
	private ConfirmDialog logoutDialog;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(nerveProgressDialog != null && nerveProgressDialog.isShowing())
				hideProgressDialog();
			switch (msg.what) {
			case R.id.okBtn:
				toast("密码修改成功，请下次使用新的密码登录");
				break;
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			}
		}
	};
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("系统设置");
		showNavigateBackButton(false);
		
		loadLoginInfo();
	}
	
	@Override
	protected void initData() {
		shareP = getSharedPreferences(AutoLoginActivity.SHARED_FILE, MODE_PRIVATE);
	}
	
	private void loadLoginInfo(){
		passwordET.setHint("输入新密码");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		showUserInfo();
	}
	
	private void showUserInfo(){
		User u = OfficeApplication.USER;
		
		((TextView)findViewById(R.id.username)).setText(u.getName());
		((TextView)findViewById(R.id.position)).setText("[点击修改]");
		((TextView)findViewById(R.id.content)).setText(u.getSign());
		((ImageView)findViewById(R.id.header)).setImageResource(
				DepartmentData.getHeaderResource(u.getSex(), u.getHeader())
				);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.okBtn:
			if(isEmpty(passwordET)){
				toast("新密码不能为空！");
				return ;
			}
			
			showProgressDialog(null, "正在修改你的登录密码...");
			new SettingWorker().execute(v.getId());
			break;
		//注销登录
		case R.id.logout:
			logoutDialog = new ConfirmDialog(this, "注销", "你确定要注销登录吗？");
			logoutDialog.setConfirmListener(this);
			logoutDialog.show();
			break;
		
		case R.id.userLayout:
			Intent intent = new Intent(this, UserSettingActivity.class);
			startActivityForResult(intent, 1);
			break;
		}
	}
	
	/**
	 * 在这里拦截返回键的事情，就可以达到统一退出程序
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public void onConfirmClick(int position, Object obj) {
		if(position == ConfirmDialog.OK){
			Editor ed = shareP.edit();
			ed.putString(AutoLoginActivity.NAME, null);
			ed.putString(AutoLoginActivity.PASSWORD, null);
			ed.commit();
			
			toActivity(LoginActivity.class, true);
			finish();
		}
	}
	
	
	/**
	 * @project: Cloudoffice_android
	 * @file: SettingActivity.java
	 * @package: com.nerve.android.cloudoffice.app.activity
	 * @description:
	 *
	 * @author: 集成显卡	1053214511@qq.com
					IBM GDC, zengxm@cn.ibm.com
	 * @date&time: Aug 17, 2013
	 * @change log:
	 */
	private class SettingWorker extends AsyncTask<Integer, Void, Integer>{

		@Override
		protected Integer doInBackground(Integer... params) {
			Reply re = null;
			switch (params[0]) {
			case R.id.okBtn:
				re = DepartmentData.updateUser(
						OfficeApplication.getLoginId(), 
						Resource.User.UPDATE_PASSWORD, 
						passwordET.getText().toString()
						);
				break;
			}
			if(re == null)
				return WORKER_FAIL;
			
			if(re.isError()){
				Const.ERROR = re.getMessage().toString();
				return WORKER_FAIL;
			}
			
			return params[0];
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}
	}
}
